package com.xyd.Servlet;

import com.xyd.Service.ProductService;
import com.xyd.Service.ShoppingCarService;
import com.xyd.bean.Product;
import com.xyd.bean.ShoppingCar;
import com.xyd.bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet({"/AddShoppingCar", "/ListShoppingCar", "/delShoppingCar"})
public class ShoppingCarServlet extends HttpServlet {
    ShoppingCarService shoppingCarService = new ShoppingCarService();
    ProductService productService = new ProductService();


    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();

        if ("/AddShoppingCar".equals(path)) {
            doAddShoppingCar(request, response);
        } else if ("/ListShoppingCar".equals(path)) {
            doListShoppingCar(request, response);
        } else if ("/delShoppingCar".equals(path)) {
            doDelShoppingCar(request, response);
        }
    }

    public void doAddShoppingCar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String pid = request.getParameter("id");
        int id = 0;
        if (pid != null) {
            id = Integer.parseInt(pid);
        }
        Product product = productService.getProductById(id);
        User user = (User) request.getSession().getAttribute("user");
        if (user != null) {
            if (product.getStock() >= 1) {
                product.setStock(product.getStock() - 1);
                productService.updateStock(product);
            } else {
                request.getRequestDispatcher("/product-view.jsp").forward(request, response);
            }

            if (shoppingCarService.getShoppingCarById(id) == null) {
                shoppingCarService.addSoppingCar(new ShoppingCar(product.getId(), product.getName(),
                        product.getPrice(), 1, user.getUid(), user.getUname()));

            } else if (shoppingCarService.getShoppingCarById(id) != null) {
                ShoppingCar shoppingCar = shoppingCarService.getShoppingCarById(id);
                shoppingCar.setQuantity(shoppingCar.getQuantity() + 1);
                shoppingCarService.updataShoppingCarById(shoppingCar);
            }

            List<ShoppingCar> list = shoppingCarService.getShoppingCarByUid(user.getUid());

            HttpSession session = request.getSession();
            session.setAttribute("list", list);

            response.sendRedirect("shopping.jsp");
        } else {
            request.getRequestDispatcher("/login.html").forward(request, response);
        }
    }

    public void doListShoppingCar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        if (user != null) {
            List<ShoppingCar> list = shoppingCarService.getShoppingCarByUid(user.getUid());

            HttpSession session = request.getSession();
            session.setAttribute("list", list);

            response.sendRedirect("shopping.jsp");

        } else {
            request.getRequestDispatcher("/login.html").forward(request, response);
        }

    }

    public void doDelShoppingCar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sid = request.getParameter("id");
        int id = 0;
        if (sid != null) {
            id = Integer.parseInt(sid);
        }

        ShoppingCar shoppingCar = shoppingCarService.getShoppingCarById(id);

        Product product = productService.getProductById(id);

        product.setStock(product.getStock() + shoppingCar.getQuantity());
        productService.updateStock(product);

        shoppingCarService.delShoppingCarById(id);

        doListShoppingCar(request, response);

    }
}

